using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Slimes
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Menu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private static readonly int TOTAL_MENU_ITEMS = 2;
        private static readonly int TOTAL_SUBMENU_ITEMS = 6;
        private MenuSelected menu = MenuSelected.ROOT;
        private GameState gameState;


        private enum MenuSelected { ROOT, BUY, SELL }

        public static SpriteFont font;
        private int _selected = 1;
        int selected
        {
            get { return _selected; }
            set
            {
                _selected = value;
                if (_selected > TOTAL_MENU_ITEMS) _selected = 1;
                if (_selected == 0) _selected = TOTAL_MENU_ITEMS;
            }
        }
        private int _subSelected = 1;
        int buyMenuSelected
        {
            get { return _subSelected; }
            set
            {
                _subSelected = value;
                if (_subSelected > TOTAL_SUBMENU_ITEMS) _subSelected = 1;
                if (_subSelected == 0) _subSelected = TOTAL_SUBMENU_ITEMS;
            }
        }

        GamePadState oldState;

        public Menu(Game game, GameState gameState)
            : base(game)
        {
            oldState = GamePad.GetState(PlayerIndex.One);
            this.gameState = gameState;
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            GamePadState newState = GamePad.GetState(PlayerIndex.One);

            switch (menu)
            {
                case MenuSelected.ROOT:
                    // Root Menu
                    if (oldState.DPad.Up == ButtonState.Released && newState.DPad.Up == ButtonState.Pressed)
                    {
                        selected--;
                    }
                    else if (oldState.DPad.Down == ButtonState.Released && newState.DPad.Down == ButtonState.Pressed)
                    {
                        selected++;
                    }

                    if (selected == 1 && oldState.Buttons.A == ButtonState.Released && newState.Buttons.A == ButtonState.Pressed)
                    {
                        menu = MenuSelected.BUY;
                    }
                    if (selected == 2 && oldState.Buttons.A == ButtonState.Released && newState.Buttons.A == ButtonState.Pressed)
                    {
                        gameState.slimeSelected = 0;
                        if (gameState.slimeSelected != -2)
                        {
                            menu = MenuSelected.SELL;
                        }
                    }
                    break;

                case MenuSelected.BUY:
                    // The buy select menu
                    if (oldState.DPad.Up == ButtonState.Released && newState.DPad.Up == ButtonState.Pressed)
                    {
                        buyMenuSelected--;
                    }
                    else if (oldState.DPad.Down == ButtonState.Released && newState.DPad.Down == ButtonState.Pressed)
                    {
                        buyMenuSelected++;
                    }

                    if (oldState.Buttons.B == ButtonState.Released && newState.Buttons.B == ButtonState.Pressed)
                    {
                        menu = MenuSelected.ROOT;
                    }
                    else if (oldState.Buttons.A == ButtonState.Released && newState.Buttons.A == ButtonState.Pressed)
                    {
                        switch (buyMenuSelected)
                        {
                            case 1:
                                gameState.buy(Slime.SlimeColor.WHITE);
                                break;
                            case 2:
                                gameState.buy(Slime.SlimeColor.PURPLE);
                                break;
                            case 3:
                                gameState.buy(Slime.SlimeColor.BLUE);
                                break;
                            case 4:
                                gameState.buy(Slime.SlimeColor.GREEN);
                                break;
                            case 5:
                                gameState.buy(Slime.SlimeColor.YELLOW);
                                break;
                            case 6:
                                gameState.buy(Slime.SlimeColor.RED);
                                break;
                        }
                    }
                    break;

                case MenuSelected.SELL:
                    if (oldState.DPad.Up == ButtonState.Released && newState.DPad.Up == ButtonState.Pressed)
                    {
                        gameState.slimeSelected++;
                    }
                    else if (oldState.DPad.Down == ButtonState.Released && newState.DPad.Down == ButtonState.Pressed)
                    {
                        gameState.slimeSelected--;
                    }
                    
                    if (oldState.Buttons.B == ButtonState.Released && newState.Buttons.B == ButtonState.Pressed)
                    {
                        gameState.slimeSelected = -2;
                        menu = MenuSelected.ROOT;
                    }
                    else if (oldState.Buttons.A == ButtonState.Released && newState.Buttons.A == ButtonState.Pressed)
                    {
                        gameState.sellSelected();
                        menu = MenuSelected.ROOT;
                    }
                    break;
            }

            oldState = newState;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            spriteBatch.Begin();
            spriteBatch.DrawString(Game1.fontOne, "Buy", new Vector2(600, 290), (selected == 1) ? ((menu == MenuSelected.BUY)? Color.Orange: Color.Yellow) : Color.White);
            spriteBatch.DrawString(Game1.fontOne, "Sell", new Vector2(600, 340), (selected == 2) ? ((menu == MenuSelected.BUY) ? Color.Orange : Color.Yellow) : Color.White);
            //spriteBatch.DrawString(Game1.fontOne, "Options", new Vector2(600, 390), (selected == 3) ? Color.Yellow : Color.White);

            if (menu == MenuSelected.BUY)
            {
                switch (buyMenuSelected)
                {
                    case 1:
                        spriteBatch.DrawString(Game1.fontOne, "White", new Vector2(675, 290), Color.White);
                        break;
                    case 2:
                        spriteBatch.DrawString(Game1.fontOne, "Purple", new Vector2(675, 290), Color.Purple);
                        break;
                    case 3:
                        spriteBatch.DrawString(Game1.fontOne, "Blue", new Vector2(675, 290), Color.Blue);
                        break;
                    case 4:
                        spriteBatch.DrawString(Game1.fontOne, "Green", new Vector2(675, 290), Color.Green);
                        break;
                    case 5:
                        spriteBatch.DrawString(Game1.fontOne, "Yellow", new Vector2(675, 290), Color.Yellow);
                        break;
                    case 6:
                        spriteBatch.DrawString(Game1.fontOne, "Red", new Vector2(675, 290), Color.Red);
                        break;
                }
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
